package com.nihao001.gui;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.helper.HttpConnection;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ExpressTestGUI extends JFrame implements ActionListener {
	
	private JButton parseBtn;
	private JButton clearBtn;
	private JTextField encodeInput;
	private JTextField urlInput;
	private JTextField expInput;
	private JTextArea resultTextArea;

	public ExpressTestGUI() {
		super();
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
		
		this.setTitle("Jsoup表达式测试工具");
		
		JPanel panel1 = new JPanel();
		panel1.setLayout(new FlowLayout(FlowLayout.LEFT));
		panel1.add(new JLabel("网    址:"));
		urlInput =new JTextField(40);
		panel1.add(urlInput);
		
		JPanel panel2 = new JPanel();
		panel2.setLayout(new FlowLayout(FlowLayout.LEFT));
		panel2.add(new JLabel("表达式:"));
		expInput = new JTextField(40);
		panel2.add(expInput);
		
		JPanel panel3 = new JPanel();
		panel3.setLayout(new FlowLayout(FlowLayout.LEFT));
		resultTextArea = new JTextArea(10, 40);
		JScrollPane sp = new JScrollPane();
		panel3.add(new JLabel("结    果:"));
		sp.setViewportView(resultTextArea);
		panel3.add(sp);
		
		JPanel panel4 = new JPanel();
		panel4.setLayout(new FlowLayout(FlowLayout.LEFT));
		panel4.add(new JLabel("操    作:"));
		parseBtn = new JButton("解析");
		parseBtn.addActionListener(this);
		panel4.add(parseBtn);
		clearBtn = new JButton("清除");
		clearBtn.addActionListener(this);
		panel4.add(clearBtn);
		
		JPanel panel5 = new JPanel();
		panel5.setLayout(new FlowLayout(FlowLayout.LEFT));
		panel5.add(new JLabel("编码:"));
		encodeInput = new JTextField();
		encodeInput.setText("UTF8");
		panel5.add(encodeInput);
		
		this.getContentPane().add(panel1);
		this.getContentPane().add(panel2);
		this.getContentPane().add(panel5);
		this.getContentPane().add(panel4);
		this.getContentPane().add(panel3);
		
		this.pack();
		this.setVisible(true);
	}

	public static void main(String[] args) {
		new ExpressTestGUI();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == parseBtn){
			String url = urlInput.getText();
			String exp = expInput.getText();
			String encode = encodeInput.getText();
			
			if(StringUtils.isEmpty(url)){
				System.out.println("请输入网址");
				return;
			}
			if(StringUtils.isEmpty(exp)){
				System.out.println("请输入Jsoup表达式");
				return;
			}
			
			StringBuilder strBuilder = new StringBuilder();
			try {
				HttpConnection conn = (HttpConnection) Jsoup.connect(url);
				conn.header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36");
				conn.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
				conn.header("Accept-Encoding", "gzip,deflate");
				conn.header("Accept-Language", "zh,zh-CN;q=0.8,en;q=0.6,zh-TW;q=0.4");
				conn.method(Method.GET);
				
				Response response = conn.execute();
				String text = null;
				if(response != null){
					if (encode != null && !encode.trim().equals("")) {
						text = response.body(encode);
					} else {
						text = response.body();
					}
				}
				
				Document root = Jsoup.parse(text);
				Elements nodes = root.select(exp);
				if(nodes != null && nodes.size() > 0){
					for(Element ele: nodes){
						strBuilder.append(ele.outerHtml() + "\r\n");
					}
				}
			} catch (Exception e1) {
				strBuilder = new StringBuilder(e1.getMessage());
			}
			resultTextArea.setText(strBuilder.toString());
			
		}
		if(e.getSource() == clearBtn){
			resultTextArea.setText("");
		}
		
	}

}
